package org.example.dfs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/9/4 09:46
 * @apiNote
 */

public class Test00090SubsetsWithDup {

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(nums,list,res,0);
        return res;
    }

    public static void dfs(int[] nums, List<Integer> list,List<List<Integer>> res,int index){
        res.add(new ArrayList<>(list));
        if(index >= nums.length){
            return;
        }
        for(int i = index; i < nums.length; i++){
            if(i>index && nums[i] == nums[i-1]){
                continue;
            }
            list.add(nums[i]);
            dfs(nums,list,res,i+1);
            list.remove(list.size()-1);
        }
    }

}
